package com.msmall.community.common.service.impl;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.community.common.dao.LinkDao;
import com.msmall.community.common.model.Link;
import com.msmall.community.common.service.LinkManager;
import com.msmall.community.common.util.LinkUtil;

public class LinkManagerImpl extends BaseManagerImpl<Link, Long> implements LinkManager{
	
	private static final Log log = LogFactory.getLog(LinkManagerImpl.class);
	
	private LinkDao linkDao;

	public void setLinkDao(LinkDao linkDao) {
		this.linkDao = linkDao;
	}

	public void saveLink(File image, String imageFileName, Link link, String uat, Long userid){
		try{
			if(link.getLinkid() != null && link.getLinkid().longValue() != 0l){
				Link old = (Link) linkDao.get(Link.class, link.getLinkid());
				if(old != null){
					link.setImageName(link.getImageName());
					link.setImagePath(link.getImagePath());
				}
			}
			link.setCreateTime(new Date());
			saveObject(link);
			if(image != null){
				link = (Link) linkDao.get(Link.class, link.getLinkid());
				Long linkid = link.getLinkid();
				String[] pathinfo = LinkUtil.saveImage(image, imageFileName, linkid, uat);
				link.setImageName(pathinfo[1]);
				link.setImagePath(pathinfo[0]);
				linkDao.saveObject(link);
			}
		}catch(Exception e){
			log.error(e);
		}
	}

	public List<Link> loadAllEnabled() {
		try{
			return linkDao.loadAllEnabled();
		}catch(Exception e){
			log.error(e);
			return null;
		}
	}

}
